package com.controller;

import com.vo.EmpVO;
import com.vo.ResponseVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

@Controller
//@ResponseBody
// 上面的2个注解等价于下面的这一个注解
//@RestController
public class HomeController {
    @RequestMapping("/")
    public String index() {
        return "index";
    }

    @RequestMapping("/emp")
    @ResponseBody
    public EmpVO emp() {
        EmpVO e = new EmpVO(11, "cj", new Date());
        return e;
    }


    @RequestMapping("/getAll")
    @ResponseBody
    public ResponseVO getAll() {
        EmpVO e = new EmpVO(11, "cj", new Date());
        EmpVO e2 = new EmpVO(12, "cj2", new Date());
        EmpVO e3 = new EmpVO(13, "cj3", new Date());
        ArrayList<EmpVO> al = new ArrayList<>();
        al.add(e);
        al.add(e2);
        al.add(e3);
        Random random = new Random();
        int j = random.nextInt(100);
        if (j > 20) {
            throw new RuntimeException("cucuo");
        }
        ResponseVO responseVO = new ResponseVO(200, "ok", al);
        return responseVO;


    }

    @RequestMapping("/insert")
    @ResponseBody
    public ResponseVO insert(@RequestBody EmpVO empVO) {

        System.out.println(empVO);
        ResponseVO responseVO = new ResponseVO(200, "ok", true);
        return responseVO;
    }


}
